package com.meritdata.cloud.data.standard.naming.service.impl;

import com.meritdata.cloud.data.standard.naming.entity.QTdDataStandardNaming;
import com.meritdata.cloud.data.standard.naming.entity.TdDataStandardNaming;
import com.meritdata.cloud.data.standard.naming.repository.ITdDataStandardNamingRepository;
import com.meritdata.cloud.data.standard.naming.service.ITdDataStandardNamingService;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TdDataStandardNamingService implements ITdDataStandardNamingService {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ITdDataStandardNamingRepository tdDataStandardNamingRepository;

    @Override
    public TdDataStandardNaming addNamingData(TdDataStandardNaming tdDataStandardNaming) {
        return  tdDataStandardNamingRepository.save(tdDataStandardNaming);
    }

    @Override
    public void deleteNamingById(Long id) {
        tdDataStandardNamingRepository.deleteById(id);
    }

    @Override
    public void updateNamingDataById(Long id,String name,String applyObject,String ruleContent,Integer maxNameLength,String remark) {
        Optional<TdDataStandardNaming> optional = tdDataStandardNamingRepository.findById(id);
        TdDataStandardNaming tdDataStandardNaming = new TdDataStandardNaming();
        tdDataStandardNaming.setId(optional.get().getId());
        tdDataStandardNaming.setName(name);
        tdDataStandardNaming.setApplyObject(applyObject);
        tdDataStandardNaming.setRuleContent(ruleContent);
        tdDataStandardNaming.setMaxNameLength(maxNameLength);
        tdDataStandardNaming.setRemark(remark);
        tdDataStandardNaming.setCreateTime(optional.get().getCreateTime());
        tdDataStandardNaming.setUpdateTime(new Date());
        tdDataStandardNaming.setStatus(optional.get().getStatus());
        tdDataStandardNaming.setCreator(optional.get().getCreator());
        tdDataStandardNamingRepository.save(tdDataStandardNaming);
    }

    @Override
    public void updateNamingStatus(Long id, Integer status) {
        Optional<TdDataStandardNaming> optional = tdDataStandardNamingRepository.findById(id);
        TdDataStandardNaming tdDataStandardNaming = new TdDataStandardNaming();
        tdDataStandardNaming.setId(optional.get().getId());
        tdDataStandardNaming.setName(optional.get().getName());
        tdDataStandardNaming.setApplyObject(optional.get().getApplyObject());
        tdDataStandardNaming.setRuleContent(optional.get().getRuleContent());
        tdDataStandardNaming.setMaxNameLength(optional.get().getMaxNameLength());
        tdDataStandardNaming.setRemark(optional.get().getRemark());
        tdDataStandardNaming.setCreateTime(optional.get().getCreateTime());
        tdDataStandardNaming.setUpdateTime(new Date());
        tdDataStandardNaming.setStatus(status);
        tdDataStandardNaming.setCreator(optional.get().getCreator());
        tdDataStandardNamingRepository.save(tdDataStandardNaming);
    }

    @Override
    public Map selectNamingAll(Integer page, Integer rows, String name) {
        QTdDataStandardNaming qTdDataStandardNaming = QTdDataStandardNaming.tdDataStandardNaming;
        Pageable pageable = PageRequest.of(page-1,rows);
        Predicate predicate = qTdDataStandardNaming.name.like("%"+name+"%");
        Map map = new HashMap();
        List<TdDataStandardNaming> tdDataStandardNamingData = jpaQueryFactory.selectFrom(qTdDataStandardNaming)
                .where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize()).fetch();
        long count = jpaQueryFactory.selectFrom(qTdDataStandardNaming)
                .where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize()).fetchCount();
        map.put("data",tdDataStandardNamingData);
        map.put("totalCount",count);
        return map;
    }
    public  Boolean existName(String name){
        QTdDataStandardNaming qTdDataStandardNaming = QTdDataStandardNaming.tdDataStandardNaming;
        Predicate predicate =  qTdDataStandardNaming.name.eq(name);
        long count = jpaQueryFactory.selectFrom(qTdDataStandardNaming)
                .where(predicate)
                .fetchCount();
        if (count > 0){
            return true;
        }else{
            return false;
        }
    }
}
